home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 May / com_0505_1.iso / opensource / top10 / amc_install.exe / {app} / Scripts / Culturalia+IMDB (batch).ifs < prev    next >
Encoding:
Text File  |  2004-12-21  |  33.0 KB  |  1,034 lines

  1. // SCRIPTING
  2. // Culturalia+IMDB (Batch) v1.0
  3.  
  4. (***************************************************
  5.  *  Script merged by Jose Miguel Folgueira, based  *
  6.  *  on a similar script merged by Antoine Potten   *
  7.  *                                                 *
  8.  *  Movie importation script for:                  *
  9.  *    Culturalia, http://www.culturalianet.com     *
  10.  *                                                 *
  11.  *  Original version made by David Arenillas       *
  12.  *  New version made by Antoine Potten             *
  13.  *  Contributors:                                  *
  14.  *    Jose Miguel Folgueira                        *
  15.  *    RedDwarf                                     *
  16.  *    Hades666                                     *
  17.  *    KaBeCi                                       *
  18.  *    PolloPolea                                   *
  19.  *    Moises DΘniz                                 *
  20.  *    Val                                          *
  21.  *                                                 *
  22.  *  Thanks to Culturalia's webmaster for his help  *
  23.  *  and for providing more direct access to his    *
  24.  *  database                                       *
  25.  *                                                 *
  26.  *  Movie importation script for:                  *
  27.  *      IMDB (US), http://us.imdb.com              *
  28.  *                                                 *
  29.  *  (c) 2002-2004 Antoine Potten                   *
  30.  *                          software@antp.be       *
  31. *                                                  *
  32. *  TransformTitle function from IMDB (US) Script   *
  33. *  (c) 2002-2004 Antoine Potten                    *
  34. *                          software@antp.be        *
  35. *                                                  *
  36.  *  Contributors :                                 *
  37.  *    Danny Falkov                                 *
  38.  *    Kai Blankenhorn                              *
  39.  *    lboregard                                    *
  40.  *    Ork <ork@everydayangels.net>                 *
  41.  *    Trekkie <Asimov@hotmail.com>                 *
  42.  *    Youri Heijnen                                *
  43.  *                                                 *
  44.  *  For use with Ant Movie Catalog 3.4.x           *
  45.  *  www.antp.be/software/moviecatalog              *
  46.  *                                                 *
  47.  *  This program is free software; you can         *
  48.  *  redistribute it and/or modify it under the     *
  49.  *  terms of the GNU General Public License as     *
  50.  *  published by the Free Software Foundation;     *
  51.  *  either version 2 of the License, or (at your   *
  52.  *  option) any later version.                     *
  53.  *                                                 *
  54.  ***************************************************)
  55.  
  56. program Culturalia_IMDb_Batch;
  57. const
  58.   BaseURLCulturalia = 'http://www.culturalianet.com/bus/catalogo.php';
  59.   UseLongestDescIMDB = False; // If set to False shortest description available will be imported, faster since taken from main page
  60.  
  61.   // Set the following constants to True to import field, or False to skip field (fiels to import from IMDB). By default, only the fields not available at  Culturalia are set to True.
  62.   // Pon las siguientes constantes a True para importar campo o False para no hacerlo (campos a importar de IMDB). Por defecto, s≤lo los campos no disponibles en Culturalia estßn a True.
  63.   ImportActors = False;
  64.   ImportCategory = False;
  65.   ImportComments = False;
  66.   ImportCountry = False;
  67.   ImportDescription = False;
  68.   ImportDirector = False;
  69.   ImportLength = True;
  70.   ImportLanguage = False;
  71.   ImportOriginalTitle = False;
  72.   ImportTranslatedTitle = False;
  73.   LeaveOriginalTitle = True; // True will get Translated Title, yet Original Title field will remain same
  74.   ImportPicture = False;
  75.   ImportLargePicture = False; // If set to False small pic will be imported
  76.   ImportRating = True;
  77.   ImportURL = False;
  78.   ImportYear = False;
  79.  
  80.   TitleMixedCase = False; // If true, each letter of each word of title begins with Uppercase. If false, the script transforms the titles in lowercase except first word
  81.  
  82.   ExternalPictures = False;
  83.     { True: Pictures will be stored as external files in the folder of the
  84.             catalog
  85.       False: Pictures will be stored inside the catalog (only for .amc files) }
  86.   ManualPictureSelect = False;
  87.     { True: If no Title Match found a picture selection window appears
  88.       False: Revert to IMDB picture }
  89.  
  90.   // Donde vamos a buscar en el caso de no haber introducido ni el tφtulo original ni el traducido en los campos correspondientes, sino en la ventana que
  91.   // se abre y nos lo solicita.
  92.   // 1-Titulo traducido, 2-Titulo original, 3-General
  93.   // What type of search in case we don't write the original title nor traslated one in the corresponding fields, we do it in the input box.
  94.   // 1-Translated title, 2-Original Title, 3-General
  95.   defdonde='1';
  96.  
  97. var
  98.   MovieName, Titulo: string;
  99.   MovieURL: string;
  100.   tmp: string;
  101.   donde: string;
  102.   Articles: array of string;
  103.   Index: Integer;
  104.  
  105. function FindLine(Pattern: string; List: TStringList; StartAt: Integer): Integer;
  106. var
  107.   i: Integer;
  108. begin
  109.   result := -1;
  110.   if StartAt < 0 then
  111.     StartAt := 0;
  112.   for i := StartAt to List.Count-1 do
  113.     if Pos(Pattern, List.GetString(i)) <> 0 then
  114.     begin
  115.       result := i;
  116.       Break;
  117.     end;
  118. end;
  119.  
  120. procedure AnalyzePageIMDB(Address: string);
  121. var
  122.   Page: TStringList;
  123.   LineNr: Integer;
  124.   MovieURL: string;
  125. begin
  126.   Page := TStringList.Create;
  127.   Page.Text := GetPage(Address);
  128.   if pos('<title>IMDb', Page.Text) = 0 then
  129.   begin
  130.     AnalyzeMoviePageIMDB(Page)
  131.   end
  132.   else
  133.   begin
  134.     MovieURL := AddMoviesTitles(Page, '<b>Exact Matches</b>');
  135.     if MovieURL = '' then
  136.       MovieURL := AddMoviesTitles(Page, '<b>Partial Matches</b>');
  137.     if MovieURL = '' then
  138.       MovieURL := AddMoviesTitles(Page, '<b>Approximate Matches</b>');
  139.     if MovieURL <> '' then
  140.       AnalyzePageIMDB(MovieURL);
  141.   end;
  142.   Page.Free;
  143. end;
  144.  
  145. function FindValue(BeginTag, EndTag: string; Page: TStringList; var LineNr: Integer; var Line: string): string;
  146. var
  147.   BeginPos, EndPos: Integer;
  148.   Value: string;
  149. begin
  150.   Result := '';
  151.   Value := '';
  152.   BeginPos := Pos(BeginTag, Line);
  153.   if BeginPos > 0 then
  154.   begin
  155.     BeginPos := BeginPos + Length(BeginTag);
  156.     if BeginTag = EndTag then
  157.     begin
  158.       Delete(Line,1,BeginPos-1);
  159.       BeginPos := 1;
  160.     end;
  161.     EndPos := pos(EndTag, Line);
  162.     while ((EndPos = 0) and (LineNr < Page.Count-1 )) do
  163.     begin
  164.       Value := Value + copy(Line, BeginPos, Length(Line) - BeginPos);
  165.       // Next Line
  166.       BeginPos := 1;
  167.       LineNr := LineNr + 1;
  168.       Line := Page.GetString(LineNr);
  169.       if Value = '' then
  170.         Exit;
  171.       EndPos := Pos(EndTag, Line);
  172.     end;
  173.     Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
  174.    end;
  175.   Result := Value;
  176. end;
  177.  
  178. procedure AnalyzeMoviePageIMDB(Page: TStringList);
  179. var
  180.   Line, Value, Value2, FullValue, OldOriginalTitle: string;
  181.   LineNr, Desc, i: Integer;
  182.   BeginPos, EndPos: Integer;
  183.   OldTitleParts, AllTitles: TStringList;
  184.   LongDescr: Boolean;
  185. begin
  186.   LongDescr := UseLongestDescIMDB;
  187.   if (LongDescr) and (Pos('<a href="plotsummary">', Page.Text) = 0) then
  188.     LongDescr := False;
  189.  
  190.   MovieURL := 'http://imdb.com/title/tt' + Copy(Page.Text, Pos('?pending&add=', Page.Text) + 17, 7);
  191.  
  192.   // URL
  193.   if ImportURL then
  194.     SetField(fieldURL, MovieURL);
  195.  
  196.   AllTitles := TStringList.Create;
  197.  
  198.   // Original Title & Year
  199.   if (ImportOriginalTitle) or (ImportYear) then
  200.   begin
  201.     LineNr := FindLine('<title>', Page, 0);
  202.     Line := Page.GetString(LineNr);
  203.     if LineNr > -1 then
  204.     begin
  205.       BeginPos := pos('<title>', Line);
  206.       if BeginPos > 0 then
  207.         BeginPos := BeginPos + 7;
  208.       EndPos := pos('(', Line);
  209.       if EndPos = 0 then
  210.         EndPos := Length(Line);
  211.       Value := copy(Line, BeginPos, EndPos - BeginPos - 1);
  212.       HTMLDecode(Value);
  213.       if ImportOriginalTitle then
  214.         OldOriginalTitle := GetField(fieldOriginalTitle);
  215.       if (ImportTranslatedTitle) and not (LeaveOriginalTitle) then
  216.         SetField(fieldOriginalTitle, Value);
  217.       BeginPos := pos('(', Line) + 1;
  218.       if BeginPos > 0 then
  219.       begin
  220.         EndPos := Pos('/I', Line);
  221.         if EndPos < BeginPos then
  222.           EndPos := pos(')', Line);
  223.         Value := copy(Line, BeginPos, EndPos - BeginPos);
  224.         if ImportYear then
  225.           SetField(fieldYear, Value);
  226.       end;
  227.     end;
  228.   end;
  229.  
  230.   // Translated Title
  231.   if ImportTranslatedTitle then
  232.   begin
  233.     OldTitleParts := TStringList.Create;
  234.     // Tokenize OldOriginalTitle while removing certain chars/common words ("the", "of")
  235.     Value := AnsiUpperCase(OldOriginalTitle);
  236.     Value := StringReplace(StringReplace(Value, ',', ' '), ':', ' ');
  237.     Value := StringReplace(StringReplace(Value, '(', ' '), ')', ' ');
  238.     Value := StringReplace(StringReplace(Value, 'OF', ' '), 'THE', ' ');
  239.     repeat
  240.       Value := StringReplace(Value, '  ', ' ');
  241.     until Pos('  ', Value) = 0;
  242.     Value := StringReplace(Trim(Value), ' ', ',');
  243.     // Value now contains the original title (comma-separated) that was filled in before running the script
  244.     Value2 := '';
  245.     for i := 1 to Length(Value) do
  246.     begin
  247.       if Pos(',', Copy(Value, i, 1)) = 0 then
  248.         Value2 := Value2 + Copy(Value, i, 1);
  249.       if (Pos(',', Copy(Value, i, 1)) = 1) or (i = Length(Value)) then
  250.       begin
  251.         OldTitleParts.Add(Value2); // put each comma-separated value from Value into a separate string in TitleParts
  252.         Value2 := '';
  253.       end;
  254.     end;
  255.     for i := 0 to OldTitleParts.Count - 1 do
  256.     // Begin comparing title parts (from the title originally filled in by moviedb owner) with
  257.     // the 'true' Original Title (extracted from IMDb) to see if it's a foreign title and needs a Translated Title
  258.     begin
  259.       if Pos(OldTitleParts.GetString(i), AnsiUpperCase(GetField(fieldOriginalTitle))) <= 0 then
  260.       begin // no match, must be a foreign title
  261.         LineNr := FindLine('Also Known As', Page, 0);
  262.         if LineNr > -1 then
  263.         begin
  264.           Line := Page.GetString(LineNr);
  265.           if Pos('Also Known As', Line) > 0 then
  266.           begin
  267.             BeginPos := Pos('Also Known As', Line) + 26;
  268.             Value := Copy(Line, BeginPos, Length(Line) - BeginPos - 4);
  269.             Value := StringReplace(Value, '<br>', '/ ');
  270.             HTMLDecode(Value);
  271.             SetField(fieldTranslatedTitle, Trim(Value));
  272.           end;
  273.         end;
  274.         Break;
  275.       end;
  276.     end;
  277.     OldTitleParts.Free;
  278.   end;
  279.  
  280.   // Rating
  281.   if ImportRating then
  282.   begin
  283.     LineNr := FindLine('User Rating:', Page, 0);
  284.     if LineNr > -1 then
  285.     begin
  286.       Line := Page.GetString(LineNr + 4);
  287.       if Pos('/10', Line) > 0 then
  288.       begin
  289.         BeginPos := pos('<b>', Line) + 3;
  290.         Value := IntToStr(Round(StrToInt(StrGet(Line, BeginPos), 0) + (StrToInt(StrGet(Line, BeginPos + 2), 0) / 10)));
  291.         SetField(fieldRating, Value);
  292.       end;
  293.     end;
  294.   end;
  295.  
  296.   // Language
  297.   LineNr := FindLine('Language:', Page, 0);
  298.   if LineNr > -1 then
  299.   begin
  300.     Line := Page.GetString(LineNr + 1);
  301.     BeginPos := pos('/">', Line) + 3;
  302.     EndPos := pos('</a>', Line);
  303.     if EndPos = 0 then
  304.       EndPos := Length(Line);
  305.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  306.     if ImportLanguage then
  307.       SetField(fieldLanguages, Value);
  308.   end;
  309.  
  310.   if ImportPicture then
  311.     GetMoviePicture(Value, Page, AllTitles);
  312.   AllTitles.Free;
  313.  
  314.   // Director
  315.   if ImportDirector then
  316.   begin
  317.     LineNr := FindLine('Directed by', Page, 0);
  318.     if LineNr > -1 then
  319.     begin
  320.       FullValue := '';
  321.       Line := Page.GetString(LineNr + 1);
  322.       repeat
  323.         BeginPos := pos('">', Line) + 2;
  324.         EndPos := pos('</a>', Line);
  325.         Value := copy(Line, BeginPos, EndPos - BeginPos);
  326.         if (Value <> '(more)') and (Value <> '') then
  327.         begin
  328.           if FullValue <> '' then
  329.             FullValue := FullValue + ', ';
  330.           FullValue := FullValue + Value;
  331.         end;
  332.         Delete(Line, 1, EndPos);
  333.       until Pos('</a>', Line) = 0;
  334.       HTMLDecode(FullValue);
  335.       SetField(fieldDirector, FullValue);
  336.     end;
  337.   end;
  338.  
  339.   // Actors
  340.   if ImportActors then
  341.   begin
  342.     LineNr := FindLine('ast overview', Page, 0);
  343.     if LineNr = -1 then
  344.       LineNr := FindLine('redited cast', Page, 0);
  345.     if LineNr > -1 then
  346.     begin
  347.       FullValue := '';
  348.       Line := Page.GetString(LineNr);
  349.       repeat
  350.         BeginPos := Pos('<td valign="top">', Line);
  351.         if BeginPos > 0 then
  352.         begin
  353.           Delete(Line, 1, BeginPos);
  354.           Line := copy(Line, 25, Length(Line));
  355.           BeginPos := pos('">', Line) + 2;
  356.           EndPos := pos('</a>', Line);
  357.           if EndPos = 0 then
  358.             EndPos := Pos('</td>', Line);
  359.           Value := copy(Line, BeginPos, EndPos - BeginPos);
  360.           if (Value <> '(more)') and (Value <> '') then
  361.           begin
  362.             BeginPos := pos('.... </td><td valign="top">', Line);
  363.             if BeginPos > 0 then
  364.             begin
  365.               EndPos := pos('</td></tr>', Line);
  366.               BeginPos := BeginPos + 27;
  367.               Value2 := copy(Line, BeginPos, EndPos - BeginPos);
  368.               if Value2 <> '' then
  369.               begin
  370.                 Value := Value + ' (as ' + Value2 + ')';
  371.               end;
  372.             end;
  373.             if FullValue <> '' then
  374.               FullValue := FullValue + ', ';
  375.             FullValue := FullValue + Value;
  376.           end;
  377.           EndPos := Pos('</td></tr>', Line);
  378.           Delete(Line, 1, EndPos);
  379.         end else
  380.         begin
  381.           Line := '';
  382.         end;
  383.       until Line = '';
  384.       HTMLDecode(FullValue);
  385.       SetField(fieldActors, FullValue);
  386.     end;
  387.   end;
  388.  
  389.   // Country
  390.   if ImportCountry then
  391.   begin
  392.     LineNr := FindLine('Country:', Page, 0);
  393.     if LineNr > -1 then
  394.     begin
  395.       Line := Page.GetString(LineNr + 1);
  396.       BeginPos := pos('/">', Line) + 3;
  397.       EndPos := pos('</a>', Line);
  398.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  399.       HTMLDecode(Value);
  400.       SetField(fieldCountry, Value);
  401.     end;
  402.   end;
  403.  
  404.   // Category
  405.   if ImportCategory then
  406.   begin
  407.     LineNr := FindLine('Genre:', Page, 0);
  408.     if LineNr > -1 then
  409.     begin
  410.       Line := Page.GetString(LineNr + 1);
  411.       BeginPos := pos('/">', Line) + 3;
  412.       EndPos := pos('</a>', Line);
  413.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  414.       HTMLDecode(Value);
  415.       SetField(fieldCategory, Value);
  416.     end;
  417.   end;
  418.  
  419.   //Description
  420.   if ImportDescription then
  421.   begin
  422.     LineNr := FindLine('Plot Summary:', Page, 0);
  423.     if LineNr < 1 then
  424.       LineNr := FindLine('Plot Outline:', Page, 0);
  425.     if LineNr > -1 then
  426.     begin
  427.       Line := Page.GetString(LineNr);
  428.       BeginPos := pos('</b>', Line) + 5;
  429.       EndPos := pos('<a href="/rg/', Line);
  430.       if EndPos < 1 then
  431.       begin
  432.         Line := Line + Page.GetString(LineNr+1);
  433.         EndPos := pos('<a href="/rg/', Line);
  434.         if EndPos < 1 then
  435.           EndPos := pos('<br><br>', Line);
  436.         if EndPos < 1 then
  437.           EndPos := Length(Line);
  438.       end;
  439.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  440.       HTMLDecode(Value);
  441.       HTMLRemoveTags(Value);
  442.       if UseLongestDescIMDB then
  443.         SetField(fieldDescription, GetDescriptions(MovieURL + 'plotsummary'))
  444.       else
  445.         SetField(fieldDescription, Value);
  446.     end;
  447.   end;
  448.  
  449.   // Comments
  450.   if ImportComments then
  451.   begin
  452.     LineNr := FindLine('<b>Summary:</b>', Page, 0);
  453.     if LineNr > -1 then
  454.     begin
  455.       Value := '';
  456.       repeat
  457.         LineNr := LineNr + 1;
  458.         Line := Page.GetString(LineNr);
  459.         EndPos := Pos('</blockquote>', Line);
  460.         if EndPos = 0 then
  461.           EndPos := Length(Line)
  462.         else
  463.           EndPos := EndPos - 1;
  464.         Value := Value + Copy(Line, 1, EndPos) + ' ';
  465.       until Pos('</blockquote>', Line) > 0;
  466.       HTMLDecode(Value);
  467.       Value := StringReplace(Value, '<br>', #13#10);
  468.       Value := StringReplace(Value, #13#10+' ', #13#10);
  469.       SetField(fieldComments, Value);
  470.     end;
  471.   end;
  472.  
  473.   // Length
  474.   if ImportLength then
  475.   begin
  476.     LineNr := FindLine('Runtime:', Page, 0);
  477.     if LineNr > -1 then
  478.     begin
  479.       Line := Page.GetString(LineNr + 1);
  480.       EndPos := pos(' min', Line);
  481.       if EndPos = 0 then
  482.         EndPos := pos('  /', Line);
  483.       if EndPos = 0 then
  484.         EndPos := Length(Line);
  485.       if Pos(':', Line) < EndPos then
  486.         BeginPos := Pos(':', Line) + 1
  487.       else
  488.         BeginPos := 1;
  489.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  490.       SetField(fieldLength, Value);
  491.     end;
  492.   end;
  493.  
  494.   DisplayResults;
  495. end;
  496.  
  497. procedure GetMoviePicture(Language: string; Page, AllTitles: TStringList);
  498. var
  499.   Line, Value, Value2, Aka, PictureAddress: string;
  500.   AmazonPage: TStringList;
  501.   FoundOnAmazon, PickTreeSelected, PictureAvailable: Boolean;
  502.   TitleRef, ImgRef, NoImage: string;
  503.   LineNr, BeginPos, EndPos, PickTreeCount, ParagraphIndex, Index, TitleLine, LastMatch: Integer;
  504. begin
  505.   FoundOnAmazon := False;
  506.  
  507.   if ImportLargePicture then
  508.   begin
  509.     // Find Alternate Titles for Movies which are not in English
  510.     Aka := '';
  511.     if Language <> 'English' Then
  512.     begin
  513.       LineNr:= FindLine('Also Known As',Page,0);
  514.       EndPos:=0;
  515.       if LineNr > -1 then
  516.       begin
  517.         Line := Page.GetString(LineNr);
  518.         repeat
  519.           Aka:=FindValue('<br>','<br>',Page,LineNr,Line);
  520.           if Aka <> '' then
  521.           begin
  522.             BeginPos:=1;
  523.             EndPos:=Pos('(',Line);
  524.             if EndPos = 0 then
  525.               EndPos := Length(Aka);
  526.             Value := copy(Aka, BeginPos, EndPos - BeginPos - 1);
  527.             Value:=TransFormIMDBTitle(Value);
  528.             AllTitles.Add(Value);
  529.           end;
  530.         until (Pos('</td>', Line) > 0) or (Pos('Runtime', Line) > 0) or (Pos('MPAA', Line) > 0 ) or (Pos('Country', Line) > 0) or (Pos('Certification', Line) > 0);
  531.       end;
  532.     end;
  533.  
  534.     TitleRef:='dvd>';
  535.     ImgRef:='dvd><img';
  536.     NoImage:='/icons/dvd-no-image.gif';
  537.     LineNr := FindLine('title="DVD available at Amazon.com"', Page, 0);
  538.     if LineNr = -1 then
  539.     begin
  540.       LineNr := FindLine('title="VHS available at Amazon.com"', Page, 0);
  541.       if LineNr > -1 then
  542.       begin
  543.         TitleRef:='video>';
  544.         ImgRef:='video><img';
  545.         NoImage:='/icons/video-no-image.gif';
  546.       end;
  547.     end;
  548.  
  549.     if LineNr > -1 then
  550.     begin
  551.       Line := Page.GetString(LineNr);
  552.       if(TitleRef='dvd>') then
  553.       begin
  554.         EndPos := pos('title="DVD', Line);
  555.         BeginPos := pos('title="VHS', Line);
  556.         while (BeginPos > 0) and (BeginPos<EndPos) do
  557.         begin
  558.           Delete(Line, 1, BeginPos+1);
  559.           BeginPos := pos('title="VHS', Line);
  560.         end;
  561.       end;
  562.       BeginPos := Pos('href="', Line) + 5;
  563.       Delete(Line, 1, BeginPos);
  564.       EndPos := Pos('"', Line);
  565.       Value := Copy(Line, 1, EndPos - 1);
  566.       AmazonPage := TStringList.Create;
  567.       AmazonPage.Text := GetPage('http://us.imdb.com' + Value);
  568.  
  569.       // Original Title
  570.       Value2 := AllTitles.GetString(0);
  571.       Value2 := TransFormIMDBTitle(Value2);
  572.  
  573.       PickTreeClear;
  574.       PickTreeCount := 0;
  575.       PickTreeAdd('Available Titles for matching a picture to: ' + Value2, '');
  576.  
  577.       ParagraphIndex := 1;
  578.       LineNr := 0;
  579.       LastMatch := -1;
  580.       TitleLine := -1;
  581.       repeat
  582.         LineNr := FindLine('<b>'+IntToStr(ParagraphIndex)+'.', AmazonPage, LineNr);
  583.  
  584.         if LineNr > -1 then
  585.         begin
  586.           TitleLine:=LineNr;
  587.           Value:='';
  588.           PictureAvailable:=False;
  589.           repeat
  590.             TitleLine:=TitleLine +1;
  591.             Line:= AmazonPage.GetString(TitleLine);
  592.             BeginPos:=0;
  593.             if Pos(TitleRef,Line) > 0 then
  594.             begin
  595.               if Pos(ImgRef,Line) = 0 then
  596.               begin
  597.                 for Index:=0 to AllTitles.Count -1 do
  598.                 begin
  599.                   Value2:=AllTitles.GetString(Index);
  600.                   BeginPos:=Pos(Value2,Line);
  601.                   if BeginPos > 0 then
  602.                     Break;
  603.                 end;
  604.                 // Match not found
  605.                 if BeginPos = 0 then
  606.                 begin
  607.                   BeginPos:=Pos(TitleRef,Line)+Length(TitleRef);
  608.                   EndPos:=Pos('</a>',Line);
  609.                   Value:=Copy(Line,BeginPos,EndPos-BeginPos);
  610.                 end;
  611.               end
  612.               else
  613.               begin
  614.                 PictureAvailable:=(Pos(NoImage,Line) = 0);
  615.                 PictureAddress:=IntToStr(TitleLine);
  616.               end;
  617.             end;
  618.             if BeginPos > 0 then
  619.               Break;
  620.           until (Pos('</table>',Line ) > 0);
  621.  
  622.           // Try to Find a Title Match
  623.           if Pos(Value2,Line) > 0 then
  624.           begin
  625.             // Compare Current Title to Original
  626.             BeginPos := Pos(TitleRef, Line) + Length(TitleRef) -1;
  627.             Delete(Line, 1, BeginPos);
  628.             EndPos:= Pos('(',Line);
  629.             if EndPos = 0 Then
  630.               EndPos := Pos('</a>', Line);
  631.             Value := Copy(Line, 1, EndPos - 1);
  632.             Value:= Trim(Value);
  633.             if Value = Value2 then
  634.             begin
  635.               if PictureAvailable then
  636.                 LastMatch:=LineNr;
  637.                 //Break
  638.             end;
  639.           end;
  640.           if PictureAvailable then
  641.           begin
  642.             PickTreeAdd(Value,PictureAddress);
  643.             PickTreeCount:=PickTreeCount+1;
  644.           end;
  645.         end;
  646.         ParagraphIndex:=ParagraphIndex+1;
  647.       until (LineNr = -1);
  648.       LineNr:=LastMatch;
  649.       if (LineNr = -1) then
  650.       begin
  651.         // Handle Amazon Page Redirection(s)
  652.         LineNr:= FindLine('You clicked on this item',AmazonPage,0);
  653.         if (LineNr = -1) then
  654.           LineNr:=FindLine('Customers who bought',AmazonPage,0);
  655.         // Display the Picture Selection Window
  656.         if (LineNr = -1) and ManualPictureSelect and (PickTreeCount > 0) then
  657.         begin
  658.           PickTreeSelected:=PickTreeExec(PictureAddress);
  659.           if PickTreeSelected then
  660.             LineNr:=StrToInt(PictureAddress,0);
  661.         end;
  662.         if (LineNr > -1 ) then
  663.         begin
  664.           LineNr := FindLine('src="http://images.amazon.com/images/P/',AmazonPage, LineNr);
  665.           if not PickTreeSelected then
  666.             TitleLine:= FindLine('/exec/obidos/ASIN/',AmazonPage, 0);
  667.           if (LineNr > TitleLine) then
  668.             LineNr:=-1;
  669.           if LineNr > -1 then
  670.           begin
  671.             Line := AmazonPage.GetString(LineNr);
  672.             BeginPos := Pos('src="http://images.amazon.com/images/P/', Line) + 4;
  673.             Delete(Line, 1, BeginPos);
  674.             EndPos := Pos('"', Line);
  675.             Value := Copy(Line, 1, EndPos - 1);
  676.             Value := StringReplace(Value, 'TZZZZZZZ', 'LZZZZZZZ');
  677.             Value := StringReplace(Value, 'THUMBZZZ', 'LZZZZZZZ');
  678.             GetPicture(Value, ExternalPictures);
  679.             FoundOnAmazon := True;
  680.           end;
  681.         end;
  682.       end
  683.       else
  684.       begin
  685.         LineNr := FindLine('http://images.amazon.com/images/P/', AmazonPage, LineNr);
  686.         if LineNr < TitleLine then
  687.         begin
  688.           Line := AmazonPage.GetString(LineNr);
  689.           BeginPos := Pos('src="', Line) + 4;
  690.           Delete(Line, 1, BeginPos);
  691.           EndPos := Pos('"', Line);
  692.           Value := Copy(Line, 1, EndPos - 1);
  693.           Value := StringReplace(Value, 'THUMBZZZ', 'LZZZZZZZ');
  694.           GetPicture(Value, ExternalPictures);
  695.           FoundOnAmazon := True;
  696.         end;
  697.       end;
  698.       AmazonPage.Free;
  699.     end;
  700.   end; // if ImportLargePicture
  701.  
  702.   if not FoundOnAmazon then
  703.   begin
  704.     {  not found on Amazon, so taking what's available directly on IMDB.  }
  705.     LineNr := FindLine('<img border="0" alt="cover"', Page, 0);
  706.     if LineNr > -1 then
  707.     begin
  708.       Line := Page.GetString(LineNr);
  709.       BeginPos := pos('src="', Line) + 4;
  710.       Delete(Line, 1, BeginPos);
  711.       EndPos := pos('"', Line);
  712.       Value := copy(Line, 1, EndPos - 1);
  713.       GetPicture(Value, ExternalPictures);
  714.     end;
  715.   end;
  716. end;
  717.  
  718. function TransformTitle(Title: string): string;
  719. var
  720.   BeginPos, EndPos: Integer;
  721.   Value: string;
  722.   Words: array of string;
  723.   Articles: array of string;
  724.   Replace,Original: string;
  725.   Index, CommaCount: Integer;
  726. Begin
  727.   // Original Title
  728.   Result:=Title;
  729.  
  730.   Setarraylength(Words,11);
  731.   Words[0]:=' In ';
  732.   Words[1]:=' On ';
  733.   Words[2]:=' Of ';
  734.   Words[3]:=' As ';
  735.   Words[4]:=' The ';
  736.   Words[5]:=' At ';
  737.   Words[6]:=' And A ';
  738.   Words[7]:=' And ';
  739.   Words[8]:=' An ';
  740.   Words[9]:=' To ';
  741.   Words[10]:=' For ';
  742.  
  743.   SetArrayLength(Articles,35);
  744.   Articles[0]:=' The';
  745.   Articles[1]:=' a';
  746.   Articles[2]:=' An';
  747.   Articles[3]:=' Le';
  748.   Articles[4]:=' L''';
  749.   Articles[5]:=' Les';
  750.   Articles[6]:=' Der';
  751.   Articles[7]:=' Das';
  752.   Articles[8]:=' Die';
  753.   Articles[9]:=' Des';
  754.   Articles[10]:=' Dem';
  755.   Articles[11]:=' Den';
  756.   Articles[12]:=' Ein';
  757.   Articles[13]:=' Eine';
  758.   Articles[14]:=' Einen';
  759.   Articles[15]:=' Einer';
  760.   Articles[16]:=' Eines';
  761.   Articles[17]:=' Einem';
  762.   Articles[18]:=' Il';
  763.   Articles[19]:=' Lo';
  764.   Articles[20]:=' La';
  765.   Articles[21]:=' I';
  766.   Articles[22]:=' Gli';
  767.   Articles[23]:=' Le';
  768.   Articles[24]:=' Uno';
  769.   Articles[25]:=' Una';
  770.   Articles[26]:=' Un''';
  771.   Articles[27]:=' O';
  772.   Articles[28]:=' Os';
  773.   Articles[29]:=' As';
  774.   Articles[30]:=' El';
  775.   Articles[31]:=' Los';
  776.   Articles[32]:=' Las';
  777.   Articles[33]:=' Unos';
  778.   Articles[34]:=' Unas';
  779.  
  780.   // Count the Comma in The Title
  781.   CommaCount := 0;
  782.   EndPos := 0;
  783.   Value := Title;
  784.   repeat
  785.      BeginPos := Pos(',', Value);
  786.      if BeginPos > 0 then
  787.      begin
  788.        Delete(Value, 1, BeginPos);
  789.        CommaCount := CommaCount + 1;
  790.        EndPos := EndPos + BeginPos;
  791.      end;
  792.   until( Pos(',',Value) = 0);
  793.  
  794.   // Compare the Article to a list of known ones
  795.   for Index := 0 to 34 do
  796.   begin
  797.     if Pos(Articles[Index], Value) <> 0 then
  798.     begin
  799.        CommaCount := 1;
  800.        BeginPos := EndPos;
  801.        Break;
  802.     end;
  803.   end;
  804.  
  805.   if (BeginPos > 0) and (CommaCount = 1) then
  806.   begin
  807.     Value := Copy(Title, BeginPos + 1, Length(Title));
  808.     Value := Trim(Value);
  809.     Result := Value + ' ' + Copy(Title, 1, BeginPos - 1);
  810.   end;
  811.  
  812.   BeginPos := Pos(': ', Result);
  813.   if BeginPos > 0 then
  814.     Result := StringReplace(Result, ': ', ' - ');
  815.  
  816.   Result := AnsiMixedCase(Result, ' ');
  817.  
  818.   for Index := 0 to 10 do
  819.   begin
  820.     if Pos(Words[Index],Result) <> 0 then
  821.     begin
  822.       Original := Words[Index];
  823.       Replace := AnsiLowerCase(Original);
  824.       Result := StringReplace(Result, Original, Replace);
  825.     end;
  826.   end;
  827.  
  828.   Result := StringReplace(Result, ' - the ', ' - The ');
  829.   Result := Trim(Result);
  830. end;
  831.  
  832. function GetDescriptions(Address: string): string;
  833. var
  834.   Line, Value: string;
  835.   LineNr: Integer;
  836.   BeginPos, EndPos,Longest: Integer;
  837.   Page: TStringList;
  838. begin
  839.   Result := '';
  840.   Longest := 0;
  841.   Page := TStringList.Create;
  842.   Page.Text := GetPage(Address);
  843.   LineNr := FindLine('<p class="plotpar">', Page, 0);
  844.   while LineNr > -1 do
  845.   begin
  846.     Value := '';
  847.     repeat
  848.       Line := Page.GetString(LineNr);
  849.       BeginPos := pos('"plotpar">', Line);
  850.       if BeginPos > 0 then
  851.         BeginPos := BeginPos + 10
  852.       else
  853.         BeginPos := 1;
  854.       EndPos := pos('</p>', Line);
  855.       if EndPos < 1 then
  856.         EndPos := Length(Line) + 1;
  857.       if Value <> '' then
  858.         Value := Value + ' ';
  859.       Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
  860.       LineNr := LineNr + 1;
  861.     until (pos('</p>', Line) > 0) or (LineNr = Page.Count);
  862.     HTMLDecode(Value);
  863.     HTMLRemoveTags(Value);
  864.     PickListAdd(Value);
  865.  
  866.     if Length(Value) > Longest then
  867.     begin
  868.       Result := Value;
  869.       Longest := Length(Value);
  870.     end;
  871.  
  872.     LineNr := FindLine('<p class="plotpar">', Page, LineNr);
  873.   end;
  874.   Page.Free;
  875. end;
  876.  
  877. function AddMoviesTitles(Page: TStringList; Tag: string): string;
  878. var
  879.   Line: string;
  880.   LineNr: Integer;
  881.   StartPos: Integer;
  882. begin
  883.   Result := '';
  884.   LineNr := FindLine(tag, Page, 0);
  885.   if LineNr > -1 then
  886.   begin
  887.     Line := Page.GetString(LineNr);
  888.     HTMLRemoveTags(Line);
  889.     PickTreeAdd(Trim(Line), '');
  890.     LineNr := LineNr + 5;
  891.     Line := Page.GetString(LineNr);
  892.     StartPos := pos('href="', Line) + 5;
  893.     Delete(Line, 1, StartPos);
  894.     Result := Copy(Line, 1, pos('">', Line) - 1);
  895.   end;
  896. end;
  897.  
  898. procedure AnalyzePageCulturalia(Address: string);
  899. var
  900.   Page: TStringList;
  901.   LineNr: Integer;
  902.   Code: string;
  903.   TitleFound: Boolean;
  904. begin
  905.   TitleFound := False;
  906.   Page := TStringList.Create;
  907.   Page.Text := GetPage(Address);
  908.   LineNr := 1;
  909.   Page.Text := StringReplace(Page.Text, '<br>', #13#10);
  910.   if Pos('No se ha encontrado ning·n artφculo por tφtulo', Page.Text) = 0 then
  911.    begin
  912.     TitleFound := True;
  913.     Code := GetValueAfter(Page.GetString(LineNr), 'Codigo = ');     
  914.     Address := (BaseURLCulturalia + '?catalogo=1&codigo=' + Code);
  915.    end;
  916.  
  917.   if TitleFound then
  918.     AnalyzeMoviePageCulturalia(Address);
  919.   Page.Free;
  920. end;
  921.  
  922. procedure AnalyzeMoviePageCulturalia(Address: string);
  923. var
  924.   Page: TStringList;
  925.   Comments: string;
  926.   strTitle: string;
  927.   strSinopsis: string;
  928.   Line: string;
  929.   LineNr: Integer;
  930.   tmp: string;
  931. begin
  932.   Page := TStringList.Create;
  933.   Page.Text := StringReplace(GetPage(Address), '<br><br>', #13#10);
  934.   Page.Text := StringReplace(Page.Text, '<br>', #13#10);
  935.   strTitle := GetValueAfter(Page.GetString(1), 'Titulo = ');
  936.   if copy(strTitle, Length(strTitle), Length(strTitle)) = '.' then
  937.   begin
  938.     tmp := Copy(strTitle, 1, Length(strTitle) -1);
  939.   end else
  940.   begin
  941.     tmp := strTitle;
  942.   end;
  943.   SetField(fieldTranslatedTitle, TransformTitle(tmp));
  944.   tmp := GetValueAfter(Page.GetString(2), 'Titulo original = ');
  945.   SetField(fieldOriginalTitle, TransformTitle(tmp));
  946.   SetField(fieldYear, GetValueAfter(Page.GetString(3), 'A±o = '));
  947.   SetField(fieldCategory, GetValueAfter(Page.GetString(4), 'Genero = '));
  948.   SetField(fieldCountry, GetValueAfter(Page.GetString(5), 'Nacion = '));
  949.   SetField(fieldDirector, GetValueAfter(Page.GetString(6), 'Director = '));
  950.   SetField(fieldActors, GetValueAfter(Page.GetString(7), 'Actores = '));
  951.   SetField(fieldProducer, GetValueAfter(Page.GetString(8), 'Productor = '));
  952.   Comments := 'Gui≤n: ' + GetValueAfter(Page.GetString(9), 'Guion = ');
  953.   Comments := Comments + #13#10 + 'Fotografφa: ' + GetValueAfter(Page.GetString(10), 'Fotografia = ');
  954.   Comments := Comments + #13#10 + 'M·sica: ' + GetValueAfter(Page.GetString(11), 'Musica = ');
  955.   SetField(fieldComments, Comments);
  956.   LineNr := FindLine('Sinopsis = ', Page, 0);
  957.   Line := Page.GetString(LineNr);
  958.   strSinopsis := GetValueAfter(Line, 'Sinopsis = ');
  959.   LineNr := LineNr + 1;
  960.   Line := Page.GetString(LineNr);
  961.   while pos('URL = ', Line) = 0 do
  962.   begin
  963.     strSinopsis := strSinopsis + #13#10 + Line;
  964.     LineNr := LineNr + 1;
  965.     Line := Page.GetString(LineNr);
  966.   end
  967.   HTMLRemoveTags(strSinopsis);
  968.   SetField(fieldDescription, StringReplace(StringReplace(strSinopsis, '"', '"'), '"', '"'));
  969.   LineNr := FindLine('URL = ', Page, 0);
  970.   if LineNr <> -1 then
  971.     SetField(fieldURL, GetValueAfter(Page.GetString(LineNr), 'URL = '));
  972.   LineNr := FindLine('Imagen = ', Page, 0);
  973.   if LineNr <> -1 then
  974.     GetPicture(GetValueAfter(Page.GetString(LineNr), 'Imagen = '), ExternalPictures);
  975.   Page.Free;
  976. end;
  977.  
  978. function GetValueAfter(Line, Identifier: string): string;
  979. begin
  980.   if Pos(Identifier, Line) = 1 then
  981.     Result := Copy(Line, Length(Identifier)+1, Length(Line))
  982.   else
  983.     Result := '';
  984. end;
  985.  
  986. begin
  987.   SetArrayLength(Articles,11);
  988.   Articles[0]:='Lo ';
  989.   Articles[1]:='La ';
  990.   Articles[2]:='Le ';
  991.   Articles[3]:='Uno ';
  992.   Articles[4]:='Una ';
  993.   Articles[5]:='Un ';
  994.   Articles[6]:='El ';
  995.   Articles[7]:='Los ';
  996.   Articles[8]:='Las ';
  997.   Articles[9]:='Unos ';
  998.   Articles[10]:='Unas ';
  999.  
  1000.   if CheckVersion(3,4,0) then
  1001.    begin
  1002.      MovieName := GetField(fieldOriginalTitle);
  1003.      donde := '&donde=2';
  1004.      if MovieName = '' then
  1005.       begin
  1006.        MovieName := GetField (fieldTranslatedTitle);
  1007.        donde := '&donde=1';
  1008.       end
  1009.      if MovieName = '' then
  1010.       begin
  1011.        Input('Importar de Culturalia', 'Introduce el Titulo de la Pelicula:', MovieName);
  1012.        donde := '&donde=' + defdonde;
  1013.       end
  1014.      if MovieName <> '' then
  1015.        begin
  1016.         // Eliminate spanish article if exists
  1017.         for Index := 0 to 10 do
  1018.         begin
  1019.          if Pos(Articles[Index], MovieName) <> 0 then
  1020.          MovieName := copy(MovieName, length(Articles[Index]), length(MovieName));
  1021.         end;
  1022.  
  1023.  
  1024.       // Eliminate point(s) at final of MovieName before search
  1025.       tmp := MovieName;
  1026.       if Copy(tmp, Length(tmp), Length(tmp)) = '.' then
  1027.         MovieName := Copy(tmp, 1, Length(tmp) -1);
  1028.       AnalyzePageCulturalia(BaseURLCulturalia + '?catalogo=1&texto=' + UrlEncode(MovieName) + donde);
  1029.       AnalyzePageIMDB('http://us.imdb.com/Tsearch?title='+UrlEncode(GetField(fieldOriginalTitle)));
  1030.      end;
  1031.   end else
  1032.     ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.4.0)');
  1033. end.
  1034.